﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.LightSwitch;


namespace LightSwitchApplication
{
    public partial class SellOrder
    {
        partial void SubTotal_Compute(ref decimal result)
        {
            // Set result to the desired field value
            result = this.Price * this.Quantity;
        }


        partial void SellOrder_Created()
        {
            this.Quantity = 1;
        }

        partial void Quantity_Validate(EntityValidationResultsBuilder results)
        {
            // results.AddPropertyError("<Error-Message>");
            if (this.Drug != null && this.Drug.Stock < this.Quantity)
                results.AddPropertyError("The current stock is :" + this.Drug.Stock);            
        }


        partial void Drug_Validate(EntityValidationResultsBuilder results)
        {
            // results.AddPropertyError("<Error-Message>");
            //Check duplicated items
            if (Drug != null)
            {
                var dupes = from so in this.SellOrderHeader.SellOrders
                            where so.Drug != null && so.Drug.Id == this.Drug.Id && 
                                !(so.Prescription != null && this.Prescription != null && so.Prescription.Id != this.Prescription.Id)
                                && so != this
                            select so;
                if (dupes.Any())
                {
                    results.AddPropertyError("Duplicated drugs: " + this.Drug.Tag);
                    return;
                }                
            }
                        
            
            if (Prescription != null && Drug != null)
            {
                //Check refills
                if (this.Prescription.SellOrders.Count() > this.Prescription.Refills + 1)
                {
                    results.AddPropertyError("The limitation of refillment (" + this.Prescription.Refills + ") is reached.");
                    return;
                }

                /*
                //Check conflicts
                var prescriptionsInUse = from prescription in this.Prescription.Patient.Prescriptions
                                         from sellorder in prescription.SellOrders
                                         where
                                            sellorder.SellOrderHeader.CreatedTime.AddDays(prescription.TakingDays).Date > DateTime.Today &&
                                            prescription != this.Prescription
                                        select prescription;

                prescriptionsInUse.GroupBy(pres => pres.MedicalName);
                foreach(Prescription prescription in prescriptionsInUse){
                    TakingRule conflict = this.DataWorkspace.ApplicationData.TakingRulesViolated(this.Prescription.MedicalName, this.Prescription.Dosage,
                        prescription.MedicalName, prescription.Dosage).FirstOrDefault();
                    if (conflict != null)
                    {
                        results.AddPropertyError("Violated Taking Rules: " + conflict.MedicalName1 + "(Max " + conflict.MaxSafeAmount1 + ") vs " +
                            conflict.MedicalName2 + "(Max" + conflict.MaxSafeAmount2 + ")");
                        return;
                    }
                }*/
            }                                 
        }

        partial void Drug_Changed()
        {
            if (this.Drug != null)
            {
                this.Price = this.Drug.RetailPrice;
                if(this.Prescription != null)
                    this.Quantity = this.Prescription.TakingWeeks * this.Prescription.TakingTimesPerDay * (int)(this.Prescription.Dosage /
                        this.Drug.Dose);                
            }
        }
    }
}
